package pw.canyingisme.plugin.occupation;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import pw.canyingisme.plugin.config.ConfigLoader;

import java.io.IOException;
import java.util.Iterator;

public class OccupationManager {
    public static void choiceOccupation(CommandSender sender, String Occupation){
        if(occupationExits(Occupation)){
            if(ConfigLoader.hasOccupation(sender.getName())){
                sender.sendMessage("职业设置失败 原因:您已经拥有"+ConfigLoader.occupationGetName(ConfigLoader.getPlayerOccupation(sender.getName()))+"职业");
                return;
            }
            try {
                ConfigLoader.setPlayerOccupation(sender.getName(),Occupation);
                OccupationHealth.setPlayerOccupationHealth(Bukkit.getPlayer(sender.getName()));
            } catch (IOException e) {
                sender.sendMessage("职业设置失败 原因:"+e.getMessage());
                e.printStackTrace();
            }
            sender.sendMessage("你的职业已设置为"+ConfigLoader.occupationGetName(Occupation));
        }else {
            sender.sendMessage(Occupation+"职业不存在");
            return;
        }
    }
    public static boolean occupationExits(String Occupation){
        Iterator it=ConfigLoader.setGetIterator( ConfigLoader.getOccupationSet());
        String next=it.next().toString();
        if(next.equalsIgnoreCase(Occupation)){
            return true;
        }
        while (it.hasNext()){
            next=it.next().toString();
            if(next.equalsIgnoreCase(Occupation)){
                return true;
            }
        }
        return false;
    }
    public static void removeOccupation(CommandSender sender,String Player){
        if(ConfigLoader.hasOccupation(Player)){
            if(ConfigLoader.removePlayerOccupation(sender,Player)){
                OccupationHealth.removePlayerOccupationHealth(Bukkit.getPlayer(Player));
                sender.sendMessage("为玩家"+Player+"移除职业成功");
                return;
            }
            sender.sendMessage("为玩家"+Player+"移除职业失败");
            return;
        }else {
            sender.sendMessage("玩家"+Player+"没有职业");
        }
    }
    public static void resetOccupation(CommandSender sender,String Player,String Occupation){
        if(ConfigLoader.hasOccupation(Player)){
            removeOccupation(sender, Player);
            OccupationHealth.removePlayerOccupationHealth(Bukkit.getPlayer(Player));
            if(occupationExits(Occupation)){
                try {
                    ConfigLoader.setPlayerOccupation(Player,Occupation);
                } catch (IOException e) {
                    sender.sendMessage("玩家"+Player+"职业设置失败 原因:"+e.getMessage());
                    e.printStackTrace();
                }
                OccupationHealth.setPlayerOccupationHealth(Bukkit.getPlayer(sender.getName()));
                sender.sendMessage("玩家"+Player+"的职业已设置为"+ConfigLoader.occupationGetName(Occupation));
            }else {
                sender.sendMessage(Occupation+"职业不存在");
                return;
            }
        }else {
            sender.sendMessage("玩家"+Player+"没有职业");
        }
    }
}
